/**
 * 系统后台数据库连接配置JS-systemDataSource.jsp
 */

//项目路径
var strPath = window.document.location.pathname;
//var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var postPath = "/gdp";
var localhost = window.document.location.origin;
var editStart = 1;//1添加、2修改

$(function () {
    getProjectSelect();//获取所有工程
    getMenuTreeList();

    //刷新操作
    $("#btn_ref").click(function () {
        $("#tb_dataList").bootstrapTable('refresh');
    });

    //删除数据
    $("#btn_del").click(function () {
        var row = $('#tb_dataList').bootstrapTable('getSelections');
        if (row == null || row == "") {
            bootbox.alert("请选择一行!");
            return;
        }

        bootbox.confirm("确认删除?", function (result) {
            if (result == false)
                return;
            var keys = "";
            $.each(row, function (i, e) {
                keys += e.guid + ",";
            });
            keys = keys.substring(0, keys.length - 1);
            $.ajax({
                url: postPath + '/system/sysPrjMenu/deletePrjMenuConfig',
                type: 'POST',
                data: {guids: keys},
                success: function (res) {
                    if (!res.success) {
                        bootbox.alert(res.msg);
                    } else {
                        showMsg("删除成功!", "success");
                        $('#tb_dataList').bootstrapTable('refresh');
                    }
                },
                error: function () {
                    showMsg("删除失败!", "error");
                }
            });
        });
    });


    //修改操作
    $("#btn_edit").click(function () {
        cleanContent();
        var row = $('#tb_dataList').bootstrapTable('getSelections');
        if (row == null || row == "") {
            bootbox.alert("请选择一行!");
            return;
        } else if (row.length > 1) {
            bootbox.alert("请选择一行!");
            return;
        }
        $("#guid").val(row[0].guid);
        $("#menucode").val(row[0].menucode);
        $("#menutitle").val(row[0].menutitle);
        $("#pcode").val(row[0].pcode);
        projectchange(row[0].pcode);
        $("#pagecode").val(row[0].pagecode);
        $("#menudesc").val(row[0].menudesc);
        $("#remark").val(row[0].remark);
        editStart = 2;
        /*$.ajax({
            type : 'POST',
            url : postPath + '/system/sysPrjMenu/getPrjMenuByGuid',
            async : false,
            data : {guid:row[0].guid},
            success : function(res) {
            	if(res.success){
	            	$("#guid").val(res.data.guid);
	            	$("#menucode").val(res.data.menucode);
	            	$("#menutitle").val(res.data.menutitle);
	            	$("#servicecode").val(res.data.servicecode);
	            	$("#menudesc").val(res.data.menudesc);
	            	$("#remark").val(res.data.remark);
	            	editStart=2;
            	}else{
            		bootbox.alert(res.msg);
            		return;
            	}
            }
        });*/
        $("#EditContent").modal('show');

    });

    //保存数据
    $("#btn_save").click(function () {
        var validataResult = validateData();
        if (validataResult) {
            var editurl = "";
            if (editStart == 1) {
                editurl = postPath + '/system/sysPrjMenu/insertPrjMenuConfig';
            } else if (editStart == 2) {
                editurl = postPath + '/system/sysPrjMenu/updatePrjMenuConfig';
            }

            var sysPrjmenu = {
                "guid": $("#guid").val(),
                "menucode": $("#menucode").val(),
                "pcode": $("#pcode").val(),
                "parentcode": $("#hid_fatureid").val(),
                "menutitle": $("#menutitle").val(),
                "menudesc": $("#menudesc").val(),
                "remark": $("#remark").val(),
                "pagecode": $("#pagecode").val()
            };

            $.ajax({
                type: 'POST',
                url: editurl,
                async: false,
                data: JSON.stringify(sysPrjmenu),
                dataType: 'json',
                contentType: "application/json",
                success: function (res) {
                    if (res.success) {
                        showMsg("保存成功!", "success");
                        var pj = $("#ptype").val();
                        getMenuTreeList(pj);
                        $("#EditContent").modal('hide');

                    } else {
                        showMsg("保存失败,请刷新后重试!", "error");
                    }
                }
            });

        }

    });

    //二级联动根据工程找已生成页面
    $("#pcode").change(function () {
        $.ajax({
            type: 'POST',
            url: postPath + '/system/sysPrjMenu/getPagebyPcode',
            async: false,
            data: {pcode: $("#pcode").val()},
            dataType: 'json',
            success: function (result) {
                if (result != null) {
                    var str = "<option value=''>请选择</option>";
                    for (var i = 0; i < result.length; i++) {
                        str += "<option value='" + result[i].pagecode + "'>" + result[i].pagename + "</option>";
                    }
                    $("#pagecode").html(str);
                }
            },
            error: function () {
                showMsg("页面查找失败,请刷新后重试!", "error");
            }
        });
    });
    //服务选择事件改编
    $("#ptype").click(function () {
        var pj = $("#ptype").val();
        getMenuTreeList(pj);

    })

})

//添加操作
$("#btn_add").click(function () {
    //alert(1)
    cleanContent();
    $("#EditContent").modal('show');
    $("#pcode").removeAttr("disabled");
    $("#hid_fatureid").val('none');
    editStart = 1;
});

$("#btn_generator").click(function () {
    var value = $('#ptype').val();
    if (value == null || value == "") {
        bootbox.alert("请选择项目工程!");
        return;
    }
    $.ajax({
        url: localhost + '/frame-ui-web/system/pageConfig/menuMarker',
        type: 'POST',
        data: {pcode: value},
        async: false,
        success: function (data) {
            if (data.success) {
                bootbox.alert(data.msg);
            } else {
                bootbox.alert(data.msg);
            }

        },
        error: function () {
            showMsg("菜单生成异常!", "error");
        }
    });
});

//添加打开模态框
function addClick(guid, menutitle, pcode) {
    cleanContent();
    $("#EditContent").modal('show');
    $("#hid_fatureid").val(guid);
    $("#fatherName").val(menutitle);
    $("#pcode").val(pcode);
    $("#pcode").attr("disabled", true);
    editStart = 1;
}

//修改打开模态框
function updateClick(guid) {
    cleanContent();

    editStart = 2;
    $.ajax({
        type: 'POST',
        url: postPath + '/system/sysPrjMenu/getPrjMenuByGuid',
        async: false,
        data: {guid: guid},
        success: function (res) {
            if (res.success) {
                $("#guid").val(res.data.guid);
                $("#menucode").val(res.data.menucode);
                $("#menutitle").val(res.data.menutitle);
                $("#pcode").val(res.data.pcode);
                projectchange(res.data.pcode);
                $("#pagecode").val(res.data.pagecode);
                $("#hid_fatureid").val(res.data.parentcode);
                $("#fatherName").val(res.data.parentname);
                $("#menudesc").val(res.data.menudesc);
                $("#remark").val(res.data.remark);
                $("#pcode").attr("disabled", true);
                editStart = 2;
            } else {
                bootbox.alert(res.msg);
                return;
            }
        }
    });
    $("#EditContent").modal('show');
}

function delClick(guid) {
    var val = false;
    $.ajax({
        type: 'POST',
        url: postPath + '/system/sysPrjMenu/isNoneMenu',
        async: false,
        data: {guid: guid},
        success: function (res) {
            val = res;
        }
    });
    if (val) {
        bootbox.confirm("请确认删除?", function (result) {
            if (result == false)
                return;
            $.ajax({
                url: postPath + '/system/sysPrjMenu/deletePrjMenuConfig',
                type: 'POST',
                data: {guids: guid},
                success: function (res) {
                    if (!res.success) {
                        bootbox.alert(res.msg);
                    } else {
                        showMsg("删除成功!", "success");
                        $('#tb_dataList').bootstrapTable('refresh');
                        getMenuTreeList();
                    }
                },
                error: function () {
                    showMsg("删除失败!", "error");
                }
            });
        });
    } else {
        bootbox.alert("请先删除子菜单选项!");
    }
}

//菜单树形表格展示
function getMenuTreeList(pcode) {
    $(".tree-2 tbody").html("");
    $.ajax({
        type: 'POST',
        url: postPath + '/system/sysPrjMenu/getMenuList',
        data: {pcode: pcode},
        async: false,
        dataType: 'json',
        //contentType:"application/json",
        success: function (res) {
            var strtd = "";
            $.each(res.data, function (i, e) {
                if (e.parentcode == "none") {
                    strtd += "<tr class='treegrid-" + e.guid + "'>";
                } else {
                    strtd += "<tr class='treegrid-" + e.guid + " treegrid-parent-" + e.parentcode + "'>";
                }
                strtd += "<td>" + e.menucode + "</td><td>" + (e.menutitle == null ? "" : e.menutitle) + "</td><td>" + (e.pname == null ? "" : e.pname) + "</td><td>" + (e.pagecode == null ? "" : e.pagecode) + "</td><td>" + (e.menudesc == null ? "" : e.menudesc) + "</td><td>" + (e.remark == null ? "" : e.remark) + "</td>";
                //strtd+="";
                strtd += "<td><a href='javascript:;' onclick='addClick(\"" + e.guid + "\",\"" + e.menutitle + "\",\"" + e.pcode + "\");' class='btn btn-default btn-sm'><i class='fa fa-plus'></i></a>";
                strtd += "<a href='javascript:;' onclick='updateClick(\"" + e.guid + "\");' class='btn btn-default btn-sm'><i class='fa fa-edit'></i> </a> ";
                strtd += " <a href='javascript:;' onclick='delClick(\"" + e.guid + "\");' class='btn btn-default btn-sm'><i class='fa fa-remove'></i> ";
                strtd += " </a></td></tr>";
            });
            $(".tree-2 tbody").append(strtd);
        }
    });
    $('.tree-2').treegrid({
        expanderExpandedClass: 'glyphicon glyphicon-minus',
        expanderCollapsedClass: 'glyphicon glyphicon-plus'
    });
}

//二级联动根据工程找已生成的页面
function projectchange(pcode) {
    $.ajax({
        type: 'POST',
        url: postPath + '/system/sysPrjMenu/getPagebyPcode',
        async: false,
        data: {pcode: pcode},
        dataType: 'json',
        success: function (result) {
            if (result != null) {
                var str = "<option value=''>请选择</option>";
                for (var i = 0; i < result.length; i++) {
                    str += "<option value='" + result[i].pagecode + "'>" + result[i].pagename + "</option>";
                }
                $("#pagecode").html(str);
            }
        },
        error: function () {
            showMsg("页面查找失败,请刷新后重试!", "error");
        }
    });
}

//获取所有工程
function getProjectSelect() {
    $.ajax({
        type: 'POST',
        url: postPath + '/system/sysprjService/getProjectList',
        async: false,
        data: '',
        dataType: 'json',
        success: function (result) {
            if (result != null) {
                var str = "<option value=''>请选择</option>";
                for (var i = 0; i < result.length; i++) {
                    str += "<option value='" + result[i].id + "'>" + result[i].name + "</option>";
                }
                $("#pcode").html(str);
                $("#ptype").html(str);
            }
        },
        error: function () {
            showMsg("保存失败,请刷新后重试!", "error");
        }
    });
}

/**查询条件与分页数据 */
function queryParams(pageReqeust) {
    var temp = {
        order: pageReqeust.order,
        pageNo: pageReqeust.offset / pageReqeust.limit + 1,
        pageSize: pageReqeust.limit,
        seachTxt: $("#seachTxt").val(),
        pcode: $("#ptype").val(),
        //servicecode: servicecode
    };
    return temp;
}

//内容初始化
function cleanContent() {
    //维护内容清空
    $("#pcode").val("");
    $("#fatherName").val("");
    $("#hid_fatureid").val("");
    $("#guid").val("");
    $("#menucode").val("");
    $("#menutitle").val("");
    $("#menudesc").val("");
    $("#remark").val("");
    $("#pagecode").val("");
    var str = "<option value=''>请选择</option>";
    $("#pagecode").html(str);
    //验证信息清空
    $("#valide_menucode").html("");
    $("#valide_menutitle").html("");
    $("#valide_menudesc").html("");
    $("#valide_remark").html("");
}

//验证表单内容
function validateData() {
    if ($("#menutitle").val() == "") {
        $("#valide_menutitle").html("菜单名称不能为空!");
        return false;
    } else {
        $("#valide_menutitle").html("");
    }
    if ($("#pcode").val() == "") {
        $("#valide_pcode").html("请选择项目工程!");
        return false;
    } else {
        $("#valide_pcode").html("");
    }
    if ($("#menucode").val() == "") {
        $("#valide_menucode").html("菜单编码不能为空!");
        return false;
    } else if (editStart == 1) {
        var result = false;
        $.ajax({
            type: 'POST',
            url: postPath + '/system/sysPrjMenu/valPrjMenuCode',
            async: false,
            data: {menucode: $("#menucode").val(), pcode: $("#pcode").val()},
            success: function (res) {
                if (res.success == false) {
                    $("#valide_menucode").html("菜单编码已存在!");
                    result = false;
                } else {
                    result = true;
                    $("#valide_menucode").html("");
                }
            }
        });
        return result;
    } else {
        $("#valide_menucode").html("");
    }
    if ($("#menudesc").val() == "") {
        $("#valide_menudesc").html("菜单说明不能为空!");
        return false;
    } else {
        $("#valide_menudesc").html("");
    }
    return true;
}

function showMsg(text, icon) {
    $.toast().reset('all');
    $.toast({
        text: text,
        icon: icon,
        showHideTransition: 'fade',
        allowToastClose: true,
        hideAfter: 1500,
        stack: false,
        position: 'top-right',
        textAlign: 'left',
        loader: true,
        loaderBg: '#9ec600',
    });
}
